package GUILayer;

import ControlLayer.LeaseItemCtr;

import ModelLayer.RealException;
import ModelLayer.LeaseItemRecords;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.CardLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import ModelLayer.LeaseItem;

public class LeaseItemUI extends JPanel {

	private static final long serialVersionUID = 1L;

	private LeaseItemCtr leaseItemCrt;

	private JTextField showIDField;
	private JTextField showNameField;
	private JTextField showLocationField;
	private JTextField showPriceField;

	private JTextField showProductIDField;
	private JTextField showSerialNoField;
	private JTextField showStausField;

	private JTextField editIDField;
	private JTextField editLocationField;
	private JTextField editNameField;
	private JTextField editPriceField;
	private JPanel editLeaseItem;

	private DefaultTableModel tableModel;
	private DefaultTableModel tableModel2;
	private JTable table;
	private JTable table2;
	private JPanel showLeaseItem;
	private JPanel showUnit;
	private JPanel showAllLeaseItems;
	private JPanel showAllUnits;
	private JPanel LeaseItem;
	private JTextField showAllIdField;

	private JTextField createLocationField;
	private JTextField createNameField;
	private JTextField createPriceField;
	private JPanel createLeaseItem;

	private JPanel LeaseItemContainer;

	public LeaseItemUI() {
		leaseItemCrt = new LeaseItemCtr();
		initialize();
	}

	public void initialize() {

		String loggedInPosition = LoginUI.getLoggedInPosition();

		tableModel = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		tableModel.addColumn("ID");
		tableModel.addColumn("Name");
		tableModel.addColumn("NumberOfAvilableUnits");
		tableModel.addColumn("Price");
		// Separat

		tableModel2 = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		tableModel2.addColumn("Product Id");
		tableModel2.addColumn("Unit Serial No.");
		tableModel2.addColumn("Status");
		// cacat

		this.setLayout(new BorderLayout(0, 0));

		JTabbedPane subMenu = new SubMenuTabbedPane();
		this.add(subMenu, BorderLayout.CENTER);

		LeaseItem = new JPanel();
		subMenu.addTab("Lease Item", null, LeaseItem, null);
		LeaseItem.setLayout(new BorderLayout(0, 0));

		JPanel showAllMenu = new JPanel();
		LeaseItem.add(showAllMenu, BorderLayout.NORTH);
		showAllMenu.setLayout(new BorderLayout(0, 0));
		showAllMenu.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Lease Item Menu",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));

		JPanel showAllMenuLeft = new JPanel();
		showAllMenu.add(showAllMenuLeft, BorderLayout.WEST);

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("manager")) {
			JButton btnCreateNewLeaseItem = new JButton("Create new Lease Item");
			btnCreateNewLeaseItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CardLayout cl = (CardLayout) LeaseItemContainer.getLayout();
					cl.show(LeaseItemContainer, "createLeaseItem");
				}
			});
			showAllMenuLeft.add(btnCreateNewLeaseItem);
		}

		JPanel showAllMenuRight = new JPanel();
		showAllMenu.add(showAllMenuRight, BorderLayout.EAST);
		JLabel showAllIdLbl = new JLabel("ID:");
		showAllMenuRight.add(showAllIdLbl);
		showAllIdField = new JTextField();
		showAllIdField.setColumns(5);
		showAllMenuRight.add(showAllIdField);
		JButton showAllBtnSearch = new JButton("Search");
		showAllBtnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showLeaseItem(Integer.parseInt(showAllIdField.getText()));
				showAllIdField.setText("");
			}
		});
		showAllMenuRight.add(showAllBtnSearch);

		LeaseItemContainer = new JPanel();
		LeaseItem.add(LeaseItemContainer);
		LeaseItemContainer.setLayout(new CardLayout(0, 0));

		showUnit = new JPanel();
		LeaseItemContainer.add(showUnit, "showUnit");
		showUnit.setLayout(new BorderLayout(0, 0));

		JPanel showUnitInfoContainer = new JPanel();
		showUnit.add(showUnitInfoContainer, BorderLayout.NORTH);
		showUnitInfoContainer
				.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		showUnitInfoContainer.setBorder(new TitledBorder(null,
				"Lease Item Info", TitledBorder.LEADING, TitledBorder.TOP,
				null, null));

		JPanel showUnitInfo = new JPanel();
		showUnitInfo.setLayout(new BoxLayout(showUnitInfo, BoxLayout.X_AXIS));
		showUnitInfoContainer.add(showUnitInfo);

		JPanel showBottom = new JPanel();
		showUnit.add(showBottom, BorderLayout.CENTER);

		// Aici

		JPanel showUnitLeft = new JPanel();
		showUnitInfo.add(showUnitLeft);
		showUnitLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel showProductIDLabelPanel = new JPanel();
		showProductIDLabelPanel.setLayout(new BoxLayout(
				showProductIDLabelPanel, BoxLayout.X_AXIS));
		showUnitLeft.add(showProductIDLabelPanel);
		JLabel showProductIDLabel = new JLabel("Product ID");
		showProductIDLabelPanel.add(showProductIDLabel);
		showProductIDLabel.setHorizontalAlignment(SwingConstants.CENTER);
		//
		JPanel showSerialNoPanel = new JPanel();
		showSerialNoPanel.setLayout(new BoxLayout(showSerialNoPanel,
				BoxLayout.X_AXIS));
		showUnitLeft.add(showSerialNoPanel);
		JLabel showSerialNoLabel = new JLabel("Serail No.");
		showSerialNoPanel.add(showSerialNoLabel);
		showSerialNoLabel.setHorizontalAlignment(SwingConstants.CENTER);
		//

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("manager")) {
			JButton showDeleteUnit = new JButton("Delete Unit");
			showDeleteUnit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						leaseItemCrt.deleteLeaseUnit(
								Integer.parseInt(showProductIDField.getText()),
								Integer.parseInt(showSerialNoField.getText()));
						showAllUnits(Integer.parseInt(showProductIDField
								.getText()));
						JOptionPane.showMessageDialog(null,
								"Unit was deleted!", "Add Unit",
								JOptionPane.INFORMATION_MESSAGE);
					} catch (NumberFormatException e1) {
						// n-are ce gresi
						e1.printStackTrace();
					} catch (RealException e1) {
						// n-are ce gresi
						e1.printStackTrace();
					}
				}
			});
			showBottom.add(showDeleteUnit);
		}
		showBottom.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		JButton unitBack = new JButton("Back");
		showBottom.add(unitBack);
		unitBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllUnits(Integer.parseInt(showProductIDField.getText()));
			}

		});

		JPanel showStatusLabelPanel = new JPanel();
		showStatusLabelPanel.setLayout(new BoxLayout(showStatusLabelPanel,
				BoxLayout.X_AXIS));
		showUnitLeft.add(showStatusLabelPanel);
		JLabel showStatusLabel = new JLabel("Status");
		showStatusLabelPanel.add(showStatusLabel);
		showStatusLabel.setHorizontalAlignment(SwingConstants.CENTER);
		//
		// Show Right

		JPanel showUnitRight = new JPanel();
		showUnitRight.setLayout(new BoxLayout(showUnitRight, BoxLayout.Y_AXIS));
		showUnitInfo.add(showUnitRight);
		//
		JPanel showProductIDFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showUnitRight.add(showProductIDFieldPanel);
		showProductIDField = new JTextField();
		showProductIDFieldPanel.add(showProductIDField);
		showProductIDField.setHorizontalAlignment(SwingConstants.CENTER);
		showProductIDField.setEditable(false);
		showProductIDField.setColumns(5);
		//
		JPanel showSerialNoFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showUnitRight.add(showSerialNoFieldPanel);
		showSerialNoField = new JTextField();
		showSerialNoFieldPanel.add(showSerialNoField);
		showSerialNoField.setHorizontalAlignment(SwingConstants.CENTER);
		showSerialNoField.setEditable(false);
		showSerialNoField.setColumns(25);
		//
		JPanel showStatusFieldPanel = new JPanel(
				new FlowLayout(FlowLayout.LEFT));
		showUnitRight.add(showStatusFieldPanel);
		showStausField = new JTextField();
		showStatusFieldPanel.add(showStausField);
		showStausField.setHorizontalAlignment(SwingConstants.CENTER);
		showStausField.setEditable(false);
		showStausField.setColumns(25);

		showLeaseItem = new JPanel();
		LeaseItemContainer.add(showLeaseItem, "showLeaseItem");
		showLeaseItem.setLayout(new BorderLayout(0, 0));

		JPanel showLeaseItemInfoContainer = new JPanel();
		showLeaseItem.add(showLeaseItemInfoContainer, BorderLayout.NORTH);
		showLeaseItemInfoContainer.setLayout(new FlowLayout(FlowLayout.CENTER,
				5, 5));
		showLeaseItemInfoContainer.setBorder(new TitledBorder(null,
				"Lease Item Info", TitledBorder.LEADING, TitledBorder.TOP,
				null, null));

		JPanel showLeaseItemInfo = new JPanel();
		showLeaseItemInfo.setLayout(new BoxLayout(showLeaseItemInfo,
				BoxLayout.X_AXIS));
		showLeaseItemInfoContainer.add(showLeaseItemInfo);

		// show LEft

		JPanel showLeaseItemLeft = new JPanel();
		showLeaseItemInfo.add(showLeaseItemLeft);
		showLeaseItemLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel showIDLabelPanel = new JPanel();
		showIDLabelPanel.setLayout(new BoxLayout(showIDLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseItemLeft.add(showIDLabelPanel);

		JLabel showIDLabel = new JLabel("ID");
		showIDLabelPanel.add(showIDLabel);
		showIDLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showNameLabelPanel = new JPanel();
		showNameLabelPanel.setLayout(new BoxLayout(showNameLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseItemLeft.add(showNameLabelPanel);

		JLabel showNameLabel = new JLabel("Name");
		showNameLabelPanel.add(showNameLabel);
		showNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showLocationLabelPanel = new JPanel();
		showLocationLabelPanel.setLayout(new BoxLayout(showLocationLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseItemLeft.add(showLocationLabelPanel);

		JLabel showLocationLabel = new JLabel("Location");
		showLocationLabelPanel.add(showLocationLabel);
		showLocationLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showPriceLabelPanel = new JPanel();
		showPriceLabelPanel.setLayout(new BoxLayout(showPriceLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseItemLeft.add(showPriceLabelPanel);

		JLabel showPriceLabel = new JLabel("Price");
		showPriceLabelPanel.add(showPriceLabel);
		showPriceLabel.setHorizontalAlignment(SwingConstants.CENTER);

		// Show Right

		JPanel showLeaseItemRight = new JPanel();
		showLeaseItemRight.setLayout(new BoxLayout(showLeaseItemRight,
				BoxLayout.Y_AXIS));
		showLeaseItemInfo.add(showLeaseItemRight);

		JPanel showIDFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showLeaseItemRight.add(showIDFieldPanel);

		showIDField = new JTextField();
		showIDFieldPanel.add(showIDField);
		showIDField.setHorizontalAlignment(SwingConstants.CENTER);
		showIDField.setEditable(false);
		showIDField.setColumns(5);

		JPanel showNameFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showLeaseItemRight.add(showNameFieldPanel);

		showNameField = new JTextField();
		showNameFieldPanel.add(showNameField);
		showNameField.setHorizontalAlignment(SwingConstants.CENTER);
		showNameField.setEditable(false);
		showNameField.setColumns(25);

		JPanel showLocationFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showLeaseItemRight.add(showLocationFieldPanel);

		showLocationField = new JTextField();
		showLocationFieldPanel.add(showLocationField);
		showLocationField.setHorizontalAlignment(SwingConstants.CENTER);
		showLocationField.setEditable(false);
		showLocationField.setColumns(20);

		JPanel showPriceFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showLeaseItemRight.add(showPriceFieldPanel);
		showPriceField = new JTextField();
		showPriceFieldPanel.add(showPriceField);
		showPriceField.setHorizontalAlignment(SwingConstants.CENTER);
		showPriceField.setEditable(false);
		showPriceField.setColumns(15);

		JPanel showBottomMenu = new JPanel();
		showLeaseItem.add(showBottomMenu, BorderLayout.CENTER);
		showBottomMenu.setLayout(new BorderLayout(0, 0));

		JPanel showBottomMenuRight = new JPanel();
		showBottomMenu.add(showBottomMenuRight, BorderLayout.CENTER);

		JButton showPrintUnitsEdit = new JButton("Print Units");
		showPrintUnitsEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllUnits(Integer.parseInt(showIDField.getText()));
			}
		});
		showBottomMenuRight.add(showPrintUnitsEdit);

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("manager")) {
			JButton showCreateUnit = new JButton("Create Unit");
			showCreateUnit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						leaseItemCrt.createLeaseItemUnit(Integer
								.parseInt(showIDField.getText()));
						JOptionPane.showMessageDialog(null, "Unit was added!",
								"Add unit", JOptionPane.INFORMATION_MESSAGE);
					} catch (NumberFormatException e1) {
						// n-are ce gresi
						e1.printStackTrace();
					} catch (RealException e1) {
						// n-are ce gresi
						e1.printStackTrace();
					}

				}
			});
			showBottomMenuRight.add(showCreateUnit);
			JButton showBtnEdit = new JButton("Edit");
			showBtnEdit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					editLeaseItem(Integer.parseInt(showIDField.getText()));
				}
			});
			showBottomMenuRight.add(showBtnEdit);

			JButton showBtnDelete = new JButton("Delete");
			showBtnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int reply = JOptionPane
							.showConfirmDialog(
									null,
									"Are you sure you want to delete this Lease Item ?",
									"Delete LeaseItem",
									JOptionPane.YES_NO_OPTION,
									JOptionPane.WARNING_MESSAGE);
					if (reply == JOptionPane.YES_OPTION) {
						boolean success = true;
						try {
							leaseItemCrt.deleteLeaseItem(Integer
									.parseInt(showIDField.getText()));
						} catch (NumberFormatException e1) {
							success = false;
							JOptionPane.showMessageDialog(null,
									"Field value invalid", "Error",
									JOptionPane.ERROR_MESSAGE);
						} catch (RealException e1) {
							success = false;
							JOptionPane.showMessageDialog(null, e1.getError(),
									"Error", JOptionPane.ERROR_MESSAGE);
						}
						if (success) {
							JOptionPane.showMessageDialog(null,
									"The item was deleted",
									"Delete Lease Item",
									JOptionPane.INFORMATION_MESSAGE);
							showAllLeaseItems();
						}
					}
				}
			});
			showBottomMenuRight.add(showBtnDelete);

		}

		JPanel showBottomMenuLeft = new JPanel();
		showBottomMenu.add(showBottomMenuLeft, BorderLayout.WEST);

		JButton showBtnBack = new JButton("Back");
		showBtnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllLeaseItems();
			}
		});
		showBottomMenuLeft.add(showBtnBack);

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("admin")) {
			editLeaseItem = new JPanel();
			LeaseItemContainer.add(editLeaseItem, "editLeaseItem");
			editLeaseItem.setLayout(new BorderLayout(0, 0));

			JPanel editLeaseItemInfoContainer = new JPanel();
			editLeaseItem.add(editLeaseItemInfoContainer, BorderLayout.NORTH);
			editLeaseItemInfoContainer.setLayout(new FlowLayout(
					FlowLayout.CENTER, 5, 5));
			editLeaseItemInfoContainer.setBorder(new TitledBorder(null,
					"Edit Lease Item", TitledBorder.LEADING, TitledBorder.TOP,
					null, null));

			JPanel editLeaseItemInfo = new JPanel();
			editLeaseItemInfo.setLayout(new BoxLayout(editLeaseItemInfo,
					BoxLayout.X_AXIS));
			editLeaseItemInfoContainer.add(editLeaseItemInfo);

			// edit LEFT!!!!!!!!!!!!!

			JPanel editLeaseItemLeft = new JPanel();
			editLeaseItemInfo.add(editLeaseItemLeft);
			editLeaseItemLeft.setLayout(new GridLayout(0, 1, 0, 0));

			JPanel editIDLabelPanel = new JPanel();
			editIDLabelPanel.setLayout(new BoxLayout(editIDLabelPanel,
					BoxLayout.X_AXIS));
			editLeaseItemLeft.add(editIDLabelPanel);
			JLabel editIDLabel = new JLabel("ID");
			editIDLabelPanel.add(editIDLabel);
			editIDLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editNameLabelPanel = new JPanel();
			editNameLabelPanel.setLayout(new BoxLayout(editNameLabelPanel,
					BoxLayout.X_AXIS));
			editLeaseItemLeft.add(editNameLabelPanel);
			JLabel editNameLabel = new JLabel("Name");
			editNameLabelPanel.add(editNameLabel);
			editNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editLocationLabelPanel = new JPanel();
			editLocationLabelPanel.setLayout(new BoxLayout(
					editLocationLabelPanel, BoxLayout.X_AXIS));
			editLeaseItemLeft.add(editLocationLabelPanel);
			JLabel editLocationLabel = new JLabel("Location");
			editLocationLabelPanel.add(editLocationLabel);
			editLocationLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editPriceLabelPanel = new JPanel();
			editPriceLabelPanel.setLayout(new BoxLayout(editPriceLabelPanel,
					BoxLayout.X_AXIS));
			editLeaseItemLeft.add(editPriceLabelPanel);
			JLabel editPriceLabel = new JLabel("Price");
			editPriceLabelPanel.add(editPriceLabel);
			editPriceLabel.setHorizontalAlignment(SwingConstants.CENTER);

			// RIGHT EDIT

			JPanel editLeaseItemRight = new JPanel();
			editLeaseItemRight.setLayout(new BoxLayout(editLeaseItemRight,
					BoxLayout.Y_AXIS));
			editLeaseItemInfo.add(editLeaseItemRight);

			JPanel editIDFieldPanel = new JPanel(
					new FlowLayout(FlowLayout.LEFT));
			editLeaseItemRight.add(editIDFieldPanel);
			editIDField = new JTextField();
			editIDField.setEditable(false);
			editIDFieldPanel.add(editIDField);
			editIDField.setHorizontalAlignment(SwingConstants.CENTER);
			editIDField.setColumns(5);

			JPanel editNameFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseItemRight.add(editNameFieldPanel);
			editNameField = new JTextField();
			editNameFieldPanel.add(editNameField);
			editNameField.setHorizontalAlignment(SwingConstants.CENTER);
			editNameField.setColumns(15);

			JPanel editLocationFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseItemRight.add(editLocationFieldPanel);
			editLocationField = new JTextField();
			editLocationFieldPanel.add(editLocationField);
			editLocationField.setHorizontalAlignment(SwingConstants.CENTER);
			editLocationField.setColumns(15);

			JPanel editPriceFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseItemRight.add(editPriceFieldPanel);
			editPriceField = new JTextField();
			editPriceFieldPanel.add(editPriceField);
			editPriceField.setHorizontalAlignment(SwingConstants.CENTER);
			editPriceField.setColumns(15);

			JPanel editButtonsPanel = new JPanel();
			editLeaseItem.add(editButtonsPanel, BorderLayout.CENTER);
			editButtonsPanel.setLayout(new BorderLayout(0, 0));

			JPanel editCenterButtonsPanel = new JPanel();
			editButtonsPanel.add(editCenterButtonsPanel, BorderLayout.CENTER);

			JButton editBtnEdit = new JButton("Edit");
			editBtnEdit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int reply = JOptionPane.showConfirmDialog(null,
							"Are you sure you want to edit this lease item ?",
							"Edit LeaseItem", JOptionPane.YES_NO_OPTION,
							JOptionPane.WARNING_MESSAGE);
					if (reply == JOptionPane.YES_OPTION) {
						boolean success = true;
						try {
							leaseItemCrt.updateLeaseItem(
									Integer.parseInt(editIDField.getText()), 1,
									editNameField.getText());
							leaseItemCrt.updateLeaseItem(
									Integer.parseInt(editIDField.getText()), 2,
									editLocationField.getText());
							leaseItemCrt.updateLeaseItem(
									Integer.parseInt(editIDField.getText()), 3,
									editPriceField.getText());
						} catch (NumberFormatException e1) {
							success = false;
							JOptionPane.showMessageDialog(null,
									"Field value invalid", "Error",
									JOptionPane.ERROR_MESSAGE);
						} catch (RealException e1) {
							success = false;
							JOptionPane.showMessageDialog(null, e1.getError(),
									"Error", JOptionPane.ERROR_MESSAGE);
						}
						if (success) {
							JOptionPane.showMessageDialog(null,
									"The lease item was edited",
									"Edit LeaseItem",
									JOptionPane.INFORMATION_MESSAGE);
							showLeaseItem(Integer.parseInt(showIDField
									.getText()));
						}
					}
				}
			});
			editCenterButtonsPanel.add(editBtnEdit);

			JButton editBtnCancel = new JButton("Cancel");
			editBtnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					showLeaseItem(Integer.parseInt(editIDField.getText()));
				}
			});
			editCenterButtonsPanel.add(editBtnCancel);

			createLeaseItem = new JPanel();
			LeaseItemContainer.add(createLeaseItem, "createLeaseItem");
			createLeaseItem.setLayout(new BorderLayout(0, 0));

			JPanel createLeaseItemInfoContainer = new JPanel();
			createLeaseItem.add(createLeaseItemInfoContainer,
					BorderLayout.NORTH);
			createLeaseItemInfoContainer.setLayout(new FlowLayout(
					FlowLayout.CENTER, 5, 5));
			createLeaseItemInfoContainer.setBorder(BorderFactory
					.createTitledBorder("Create lease item"));

			JPanel createLeaseItemInfo = new JPanel();
			createLeaseItemInfo.setLayout(new BoxLayout(createLeaseItemInfo,
					BoxLayout.X_AXIS));
			createLeaseItemInfoContainer.add(createLeaseItemInfo);

			// create Left

			JPanel createLeaseItemLeft = new JPanel();
			createLeaseItemInfo.add(createLeaseItemLeft);
			createLeaseItemLeft.setLayout(new GridLayout(0, 1, 0, 0));

			JPanel createNameLabelPanel = new JPanel();
			createNameLabelPanel.setLayout(new BoxLayout(createNameLabelPanel,
					BoxLayout.X_AXIS));
			createLeaseItemLeft.add(createNameLabelPanel);
			JLabel createNameLabel = new JLabel("Name");
			createNameLabelPanel.add(createNameLabel);
			createNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel createLocationLabelPanel = new JPanel();
			createLocationLabelPanel.setLayout(new BoxLayout(
					createLocationLabelPanel, BoxLayout.X_AXIS));
			createLeaseItemLeft.add(createLocationLabelPanel);
			JLabel createLocationLabel = new JLabel("Location");
			createLocationLabelPanel.add(createLocationLabel);
			createLocationLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel createPriceLabelPanel = new JPanel();
			createPriceLabelPanel.setLayout(new BoxLayout(
					createPriceLabelPanel, BoxLayout.X_AXIS));
			createLeaseItemLeft.add(createPriceLabelPanel);
			JLabel createPriceLabel = new JLabel("Price");
			createPriceLabelPanel.add(createPriceLabel);
			createPriceLabel.setHorizontalAlignment(SwingConstants.CENTER);

			// create Right

			JPanel createLeaseItemRight = new JPanel();
			createLeaseItemRight.setLayout(new BoxLayout(createLeaseItemRight,
					BoxLayout.Y_AXIS));
			createLeaseItemInfo.add(createLeaseItemRight);

			JPanel createNameFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			createLeaseItemRight.add(createNameFieldPanel);
			createNameField = new JTextField();
			createNameFieldPanel.add(createNameField);
			createNameField.setHorizontalAlignment(SwingConstants.CENTER);
			createNameField.setColumns(20);

			JPanel createLocationFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			createLeaseItemRight.add(createLocationFieldPanel);
			createLocationField = new JTextField();
			createLocationFieldPanel.add(createLocationField);
			createLocationField.setHorizontalAlignment(SwingConstants.CENTER);
			createLocationField.setColumns(25);

			JPanel createPriceFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			createLeaseItemRight.add(createPriceFieldPanel);
			createPriceField = new JTextField();
			createPriceFieldPanel.add(createPriceField);
			createPriceField.setHorizontalAlignment(SwingConstants.CENTER);
			createPriceField.setColumns(15);

			JPanel createBottomMenu = new JPanel();
			createLeaseItem.add(createBottomMenu, BorderLayout.CENTER);
			createBottomMenu.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

			JButton createBtnCreate = new JButton("Create");
			createBottomMenu.add(createBtnCreate);

			createBtnCreate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean success = true;
					try {
						leaseItemCrt.createLeaseItem(
								createNameField.getText(),
								Integer.parseInt(createLocationField.getText()),
								Double.parseDouble(createPriceField.getText()));
					} catch (NumberFormatException e1) {
						success = false;
						JOptionPane.showMessageDialog(null,
								"Field value invalid", "Error",
								JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						JOptionPane.showMessageDialog(null,
								"The lease item was created",
								"Create LeaseItem",
								JOptionPane.INFORMATION_MESSAGE);
						showAllLeaseItems();
					}
				}
			});

			JButton createBtnCancel = new JButton("Cancel");
			createBtnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					showAllLeaseItems();
				}
			});
			createBottomMenu.add(createBtnCancel);

		}
		//
		showAllUnits = new JPanel();
		LeaseItemContainer.add(showAllUnits, "showAllUnits");
		showAllUnits.setLayout(new BorderLayout(0, 0));
		JPanel showAllUnitsPanel = new JPanel();
		showAllUnitsPanel.setBorder(BorderFactory
				.createTitledBorder("All lease Units"));
		showAllUnits.add(showAllUnitsPanel, BorderLayout.CENTER);

		table2 = new JTable(tableModel2);
		table2.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

		table2.setAutoCreateRowSorter(true);

		table2.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int row = table2.rowAtPoint(e.getPoint());
				if (e.getClickCount() == 2) {
					showUnit(Integer.valueOf((Integer) tableModel2.getValueAt(
							row, 0)), Integer.valueOf((Integer) tableModel2
							.getValueAt(row, 1)));
				}
			}
		});
		showAllUnitsPanel.setLayout(new BoxLayout(showAllUnitsPanel,
				BoxLayout.X_AXIS));
		JScrollPane scrollPane2 = new JScrollPane();
		showAllUnitsPanel.add(scrollPane2);
		scrollPane2.setViewportView(table2);

		JPanel Buttons = new JPanel();
		showAllUnits.add(Buttons, BorderLayout.SOUTH);
		JButton showBack = new JButton("Back");
		showBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllLeaseItems();
			}
		});
		Buttons.add(showBack);

		// Separat

		showAllLeaseItems = new JPanel();
		LeaseItemContainer.add(showAllLeaseItems, "showAllLeaseItems");
		showAllLeaseItems.setLayout(new BorderLayout(0, 0));

		JPanel showAllLeaseItemsPanel = new JPanel();
		showAllLeaseItemsPanel.setBorder(BorderFactory
				.createTitledBorder("All lease items"));
		showAllLeaseItems.add(showAllLeaseItemsPanel, BorderLayout.CENTER);

		table = new JTable(tableModel);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

		table.setAutoCreateRowSorter(true);

		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int row = table.rowAtPoint(e.getPoint());
				if (e.getClickCount() == 2) {
					showLeaseItem(Integer.valueOf((Integer) tableModel
							.getValueAt(row, 0)));
				}
			}
		});
		showAllLeaseItemsPanel.setLayout(new BoxLayout(showAllLeaseItemsPanel,
				BoxLayout.X_AXIS));

		JScrollPane scrollPane = new JScrollPane();
		showAllLeaseItemsPanel.add(scrollPane);

		scrollPane.setViewportView(table);

		showAllLeaseItems();
	}

	protected void showLeaseItem(int id) {
		boolean success = true;
		ModelLayer.LeaseItem leaseItem = null;
		try {
			leaseItem = LeaseItemRecords.getInstance().getLeaseItem(id);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			showIDField.setText(Integer.toString(leaseItem.getId()));
			showLocationField.setText(Long.toString(leaseItem.getLocation()));
			showNameField.setText(leaseItem.getName());
			showPriceField.setText(Double.toString(leaseItem.getPrice()));
			CardLayout cl = (CardLayout) (LeaseItemContainer.getLayout());
			cl.show(LeaseItemContainer, "showLeaseItem");
		}
	}

	protected void showUnit(int id, int serial) {
		boolean success = true;
		ModelLayer.LeaseItemUnit unit = null;
		try {
			unit = LeaseItemRecords.getInstance().getLeaseItem(id)
					.getUnit(serial);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			showProductIDField.setText(Integer.toString(unit.getLeaseItem()
					.getId()));
			showSerialNoField.setText(Long.toString(unit.getSerialNo()));
			showStausField.setText(String.valueOf(unit.getStatus()));
			CardLayout cl = (CardLayout) (LeaseItemContainer.getLayout());
			cl.show(LeaseItemContainer, "showUnit");
		}
	}

	protected void editLeaseItem(int id) {
		ModelLayer.LeaseItem leaseItem = null;
		boolean success = true;
		try {
			leaseItem = LeaseItemRecords.getInstance().getLeaseItem(id);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			editIDField.setText(Integer.toString(leaseItem.getId()));
			editNameField.setText(leaseItem.getName());
			editLocationField
					.setText(Integer.toString(leaseItem.getLocation()));
			editPriceField.setText(Double.toString(leaseItem.getPrice()));
			CardLayout cl = (CardLayout) (LeaseItemContainer.getLayout());
			cl.show(LeaseItemContainer, "editLeaseItem");
		}
	}

	protected void showAllLeaseItems() {
		ArrayList<ModelLayer.LeaseItem> leaseItems = LeaseItemRecords
				.getInstance().getLeaseItems();
		tableModel.setRowCount(0);
		for (LeaseItem leaseItem : leaseItems) {
			tableModel.addRow(new Object[] { leaseItem.getId(),
					leaseItem.getName(),
					leaseItemCrt.getFreeIUnits(leaseItem.getId()),
					leaseItem.getPrice() });
		}
		CardLayout cl_LeaseItemContainer = (CardLayout) (LeaseItemContainer
				.getLayout());
		cl_LeaseItemContainer.show(LeaseItemContainer, "showAllLeaseItems");
	}

	protected void showAllUnits(int id) {
		boolean success = true;
		ArrayList<ModelLayer.LeaseItemUnit> units = null;
		try {
			units = LeaseItemRecords.getInstance().getLeaseItem(id)
					.getLeaseItemUnits();
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			tableModel2.setRowCount(0);
			for (ModelLayer.LeaseItemUnit unit : units) {
				tableModel2.addRow(new Object[] { id, unit.getSerialNo(),
						unit.getStatus().toString() });
			}
			CardLayout cl_LeaseItemContainer = (CardLayout) (LeaseItemContainer
					.getLayout());
			cl_LeaseItemContainer.show(LeaseItemContainer, "showAllUnits");
		}
	}

}
